package com.woniuxy.course.controller;

import com.woniuxy.commons.dto.CourseDto;
import com.woniuxy.commons.entity.*;
import com.woniuxy.commons.exception.EtmsServiceException;
import com.woniuxy.commons.exception.course.ChapterInsertException;
import com.woniuxy.commons.exception.course.CourseAddException;
import com.woniuxy.commons.exception.course.TagInsertException;
import com.woniuxy.commons.utils.CodeUtils;
import com.woniuxy.course.service.CourseService;
import com.woniuxy.course.service.CourseTagService;
import com.woniuxy.course.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author LuoChen
 * @DateTime 2020-07-18 16:28:49
 * @Description 课程模块前端控制器
 */
@RestController
public class CourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseTagService courseTagService;

    /**
     * 查询所有的课程
     *
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.util.List < com.woniuxy.commons.dto.CourseDto>>
     * @author: LuoChen
     * @dateTime: 2020-07-18 17:30:16
     */
    @GetMapping("/all")
    public ResponseEntity<List<CourseDto>> queryAllCourses() {
        ResponseEntity<List<CourseDto>> listResponseEntity = new ResponseEntity<>();
        List<CourseDto> allCourses = courseService.findAllCourses();
        if (allCourses == null) {
            return listResponseEntity.setCode(StatusCode.GET_NOTHING).setMessage("什么也没有");
        }
        return listResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setMessage("所有的课程都在这了")
                .setData(allCourses);
    }

    /**
     * 按条件分页查询
     *
     * @param courseDto
     * @return: com.woniuxy.commons.entity.ResponseEntity<com.woniuxy.commons.entity.PageBean < com.woniuxy.commons.dto.CourseDto>>
     * @author: LuoChen
     * @dateTime: 2020-07-18 17:30:53
     */
    @RequestMapping("/condition")
    public ResponseEntity<PageBean<CourseDto>> queryCoursesByCondition(@RequestBody CourseDto courseDto) {
        ResponseEntity<PageBean<CourseDto>> listResponseEntity = new ResponseEntity<>();
        if (courseDto == null) {
            return listResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("有人在非法传参");
        }
        PageBean<CourseDto> coursesByCondition = courseService.findCoursesByCondition(courseDto);
        if (coursesByCondition == null) {
            return listResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("系统异常");
        }
        return listResponseEntity.setMessage("课程查询结果").setCode(CodeUtils.SUCCESS_CODE)
                .setData(coursesByCondition);

    }

    /**
     * 新增一门课程
     *
     * @param course
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.lang.Void>
     * @author: LuoChen
     * @dateTime: 2020-07-18 17:32:01
     */
    @PostMapping("/newCourse")
    public ResponseEntity<Void> addNewCourse(@RequestBody Course course) {
        ResponseEntity<Void> voidResponseEntity = new ResponseEntity<>();
        try {
            System.out.println("课程信息：" + course);
            if (course == null) {
                return voidResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("非法传参");
            }
            courseService.addCourse(course);
            return voidResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setMessage("新增课程成功");
        } catch (EtmsServiceException e) {
            if (e instanceof CourseAddException) {
                return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("课程添加失败");
            } else if (e instanceof TagInsertException) {
                return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("标签添加失败");
            } else if (e instanceof ChapterInsertException) {
                return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("章节添加失败");
            } else {
                return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("系统异常");
            }
        }
    }

    /**
     * 修改课程信息
     *
     * @param course
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.lang.Void>
     * @author: LuoChen
     * @dateTime: 2020-07-18 17:39:21
     */
    @PutMapping("/update")
    public ResponseEntity<Void> updateCourseInfo(@RequestBody Course course) {
        ResponseEntity<Void> voidResponseEntity = new ResponseEntity<>();
        if (course == null) {
            return voidResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("非法传参");
        }
        if (courseService.updateCourseInfo(course) == StatusCode.OPERATION_FAIL) {
            return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("系统异常");
        }
        return voidResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setMessage("修改信息成功");
    }


    /**
     * 改变课程状态
     *
     * @param cid
     * @param way
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.lang.Void>
     * @author: LuoChen
     * @dateTime: 2020-07-18 17:45:41
     */
    @PutMapping("/status/{cid}/{way}")
    public ResponseEntity<Void> changeStatus(@PathVariable("cid") int cid, @PathVariable("way") int way) {
        ResponseEntity<Void> voidResponseEntity = new ResponseEntity<>();
        if (cid == 0) {
            return voidResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("传参数出了问题");
        }
        int operation = 0;
        switch (way) {
            //上架/发布
            case 1:
                operation = courseService.courseOn(cid);
                break;
            case 2:
                operation = courseService.courseOff(cid);
                break;
            case 3:
                operation = courseService.putCourseIntoDraftsBox(cid);
                break;
            default:
                return voidResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("传参数出了问题");
        }
        if (operation == 1) {
            return voidResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setMessage("修改成功");
        }
        return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("系统异常");
    }

    /**
     * 删除一门课程
     *
     * @param cid
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.lang.Void>
     * @author: LuoChen
     * @dateTime: 2020-07-18 17:48:14
     */
    @DeleteMapping("/delete/{cid}")
    public ResponseEntity<Void> deleteCourse(@PathVariable("cid") int cid) {
        ResponseEntity<Void> voidResponseEntity = new ResponseEntity<>();
        if (cid == 0) {
            return voidResponseEntity.setCode(StatusCode.RESPONSE_NULL).setMessage("传参数出了问题");
        }
        if (courseService.deleteCourseByCid(cid) == StatusCode.OPERATION_SUCCESS) {
            return voidResponseEntity.setCode(CodeUtils.SUCCESS_CODE).setMessage("删除成功");
        }
        return voidResponseEntity.setCode(StatusCode.SERVER_ERROR).setMessage("系统异常");
    }

    /**
     * 查找所有的teachers
     *
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.util.List < com.woniuxy.commons.entity.Teacher>>
     * @author: LuoChen
     * @dateTime: 2020-08-03 15:41:46
     */
    @GetMapping("/allTeachers")
    public ResponseEntity<List<Teacher>> findAllTeachers() {
        ResponseEntity<List<Teacher>> listResponseEntity = new ResponseEntity<>();
        List<Teacher> allTeachers = courseService.findAllTeachers();
        return listResponseEntity.setCode(CodeUtils.SUCCESS_CODE)
                .setMessage("所有教师")
                .setData(allTeachers);
    }

    /**
     * 根据id查课程信息
     *
     * @param cid
     * @return: com.woniuxy.commons.entity.ResponseEntity<com.woniuxy.commons.entity.Course>
     * @author: LuoChen
     * @dateTime: 2020-08-03 15:58:12
     */
    @GetMapping("/findByCid/{cid}")
    public ResponseEntity<Course> findCourseByCid(@PathVariable("cid") int cid) {
        ResponseEntity<Course> listResponseEntity = new ResponseEntity<>();
        Course courseByCid = courseService.findCourseByCid(cid);
        return listResponseEntity.setCode(CodeUtils.SUCCESS_CODE)
                .setMessage("根据id查课程")
                .setData(courseByCid);
    }

    /**
     * 查询所有的标签
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.util.List<com.woniuxy.commons.entity.CourseTag>>
     * @author: LuoChen
     * @dateTime: 2020-08-05 01:26:52
     */
    @GetMapping("/allTags")
    public ResponseEntity<List<CourseTag>> findAllTags() {
        ResponseEntity<List<CourseTag>> listResponseEntity = new ResponseEntity<>();
        List<CourseTag> allTags = courseTagService.findAllTags();
        return listResponseEntity.setCode(CodeUtils.SUCCESS_CODE)
                .setMessage("所有tags")
                .setData(allTags);
    }
}
